import {createRouter, createWebHistory} from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Register from "../views/Register.vue";
import store from "../store"
import HelloWorld from "../components/common/HelloWorld.vue";

const routes = [
    {
        path: "/",
        redirect: "login" //默认显示推荐组件(路由的重定向)
    },
    {
        path: '/Home',
        name: 'Home',
        component: Home,
        meta: {
            requireAuth: true
        },
        children:[
            {
                path: "/HelloWorld",
                name: "HelloWorld",
                component: HelloWorld,
                meta: {
                    requireAuth: true
                }
            },
            {
                path: '/Student',
                name: 'Student',
                component: () => import("../components/student/Student.vue"),
                meta: {requireAuth: false,  visiable: true, roles: ['student']}
            },
            {
                path: '/StudentNotice',
                name: 'StudentNotice',
                component: () => import("../components/student/StudentNotice.vue"),
                meta: {requireAuth: false,  visiable: true, roles: ['student']}
            },
            {
                path: '/Student/AwardDetails',
                name: 'AwardDetails',
                component: () => import("../components/student/AwardDetails.vue"),
                meta: {requireAuth: false,  visiable: true, roles: ['student']}
            },
            {
                path: '/Student/MyAward',
                name: 'MyAward',
                component: () => import("../components/student/MyAward.vue"),
                meta: {requireAuth: false,  visiable: true, roles: ['student']}
            },
            {
                path: '/Admin',
                name: 'Admin',
                component: () => import("../components/admin/Admin.vue"),
                meta: {requireAuth: false, visiable: true, roles: ['admin']}
            },
            {
                path: '/Admin/UpdateAward',
                name: 'UpdateAward',
                component: () => import("../components/admin/UpdateAward.vue"),
                meta: {requireAuth: false, visiable: true, roles: ['admin']}
            },
            {
                path: '/Admin/AdminQuery',
                name: 'AdminQuery',
                component: () => import("../components/admin/AdminQuery.vue"),
                meta: {requireAuth: false, visiable: true, roles: ['admin']}
            },
            {
                path: '/Admin/RolesInformation',
                name: 'RolesInformation',
                component: () => import("../components/admin/RolesInformation.vue"),
                meta: {requireAuth: false, visiable: true, roles: ['admin']}
            },
            {
                path: '/Admin/DataStatistics',
                name: 'DataStatistics',
                component: () => import("../components/admin/DataStatistics.vue"),
                meta: {requireAuth: false, visiable: true, roles: ['admin']}
            },
            {
                path: '/Admin/MessageSetting',
                name: 'MessageSetting',
                component: () => import("../components/admin/MessageSetting.vue"),
                meta: {requireAuth: false, visiable: true, roles: ['admin']}
            },
            {
                path: '/Teacher',
                name: 'Teacher',
                component: () => import("../components/teacher/Teacher.vue"),
                meta: {requireAuth: false,  visiable: true,roles: ['teacher']}
            },
            {
                path: '/Teacher/Examine',
                name: 'Examine',
                component: () => import("../components/teacher/Examine.vue"),
                meta: {requireAuth: false,  visiable: true, roles: ['teacher']}
            },
            {
                path: '/Teacher/TeacherQuery',
                name: 'TeacherQuery',
                component: () => import("../components/teacher/TeacherQuery.vue"),
                meta: {requireAuth: false,  visiable: true, roles: ['teacher']}
            },
            {
                path: '/Teacher/Notice',
                name: 'Notice',
                component: () => import("../components/teacher/Notice.vue"),
                meta: {requireAuth: false,  visiable: true, roles: ['teacher']}
            },
            {
                path: '/Teacher/StudentInformation',
                name: 'StudentInformation',
                component: () => import("../components/teacher/StudentInformation.vue"),
                meta: {requireAuth: false,  visiable: true, roles: ['teacher']}
            },
        ]
    },
    {
        path: '/about',
        name: 'About',
        component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
    },
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/register',
        name: 'Register',
        component: Register
    },

]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})
router.beforeEach((to, from, next) => {
    //判断是否需要认证
    if (to.meta.requireAuth) {
        //是否登录过
        if (Object.keys(store.state.user).length !== 0) {
          next()
        } else {
          next({
            path: 'login'
          })
        }
    } else {
        next()
    }
})

export default router
